package repo

import (
	"encoding/json"
	"fmt"
	"github.com/sirupsen/logrus"
	"io/ioutil"
	"net/http"
	"strconv"
	"strings"
)

type MemberInterface interface {
	GetUserInfo(userId int64) (map[string]interface{},error) //获取用户信息
}

//返回结构体对象
func NewMember() MemberInterface {
	return &member{}
}

//member构体
type member struct {
}

func (this *member) GetUserInfo(userId int64) (map[string]interface{},error) {
	var userInfo map[string]interface{}
	idStr := strconv.FormatInt(userId,10)
	params  := "{\"userId\" : \"" + idStr + "\"}"
	req, err := http.NewRequest(http.MethodPost, "http://localhost:9090/user/userService/GetUserInfo", strings.NewReader(params))
	req.Header.Set("Content-Type", "application/json")
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		logrus.Errorf("请求rpc用户服务报错-[%s]",err)
		return userInfo,err
	}
	defer resp.Body.Close()
	bs, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		logrus.Errorf("请求rpc用户服务读取数据报错-[%s]",err)
		return userInfo,err
	}

	m := make(map[string]interface{})
	err = json.Unmarshal(bs, &m)
	if err != nil {
		return userInfo,err
	}
	fmt.Println("m:", m)
	_,ok := m["result"]
	if ok{
		return m["result"].(map[string]interface{}), nil
	}else{
		return userInfo,err
	}

}
